package com.mobileduty.pages;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.mobileduty.R;
import com.mobileduty.fragment.ApplicationFragment;
import com.mobileduty.fragment.ContactFragment;
import com.mobileduty.fragment.EquipmentFragment;
import com.mobileduty.fragment.MessageFragment;
import com.mobileduty.fragment.MoreFragment;

/**
 * 首页tab
 */

public class MainTabActivity extends AppCompatActivity {
    private Context context = this;
    private long exitTime = 0;
    private FrameLayout maintab_content;
    private RadioGroup rg_tab;
    private CoordinatorLayout id_coordinatorlayout;
    private FragmentTransaction fragmentTransaction;

    private Fragment messageFragment;
    private Fragment equipmentFragment;
    private Fragment applicationFragment;
    private Fragment contactFragment;
    private Fragment moreFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maintab_main);
        initView();
    }

    private void initView() {
        maintab_content = (FrameLayout) findViewById(R.id.maintab_content);
        rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
        id_coordinatorlayout = (CoordinatorLayout) findViewById(R.id.id_coordinatorlayout);
        rg_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_message:
                        setSelected(0);
                        break;
                    case R.id.rb_equipment:
                        setSelected(1);
                        break;
                    case R.id.rb_application:
                        setSelected(2);
                        break;
                    case R.id.rb_contact:
                        setSelected(3);
                        break;
                    case R.id.rb_more:
                        setSelected(4);
                        break;

                }
            }
        });
        setSelected(0);
    }

    //自定义一个方法，设定布局中间的FrameLayout的选择状态
    private void setSelected(int index) {
        FragmentManager fm = getSupportFragmentManager();
        fragmentTransaction = fm.beginTransaction();
        hideTransaction(fragmentTransaction);
        switch (index) {
            case 0:
                if (messageFragment == null) {
                    messageFragment = new MessageFragment();
                    fragmentTransaction.add(R.id.maintab_content, messageFragment);
                }
                fragmentTransaction.show(messageFragment);
                break;
            case 1:
                if (equipmentFragment == null) {
                    equipmentFragment = new EquipmentFragment();
                    fragmentTransaction.add(R.id.maintab_content, equipmentFragment);
                }
                fragmentTransaction.show(equipmentFragment);
                break;
            case 2:
                if (applicationFragment == null) {
                    applicationFragment = new ApplicationFragment();
                    fragmentTransaction.add(R.id.maintab_content, applicationFragment);
                }
                fragmentTransaction.show(applicationFragment);
                break;
            case 3:
                if (contactFragment == null) {
                    contactFragment = new ContactFragment();
                    fragmentTransaction.add(R.id.maintab_content, contactFragment);
                }
                fragmentTransaction.show(contactFragment);
                break;
            case 4:
                if (moreFragment == null) {
                    moreFragment = new MoreFragment();
                    fragmentTransaction.add(R.id.maintab_content, moreFragment);
                }
                fragmentTransaction.show(moreFragment);
                break;
        }
        fragmentTransaction.commit();
    }

    //隐藏fragment
    private void hideTransaction(FragmentTransaction fragmentTransaction) {
        if (messageFragment != null) {
            fragmentTransaction.hide(messageFragment);
        }
        if (equipmentFragment != null) {
            fragmentTransaction.hide(equipmentFragment);
        }
        if (applicationFragment != null) {
            fragmentTransaction.hide(applicationFragment);
        }
        if (contactFragment != null) {
            fragmentTransaction.hide(contactFragment);
        }
        if (moreFragment != null) {
            fragmentTransaction.hide(moreFragment);
        }
    }

    /**
     * 退出程序
     *
     * @param event
     * @return
     */
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
        }
        return true;
    }
}
